package guowei.guliproduct.product.controller;

import java.util.*;

import guowei.common.group.AddGroup;
import guowei.common.group.UpdateGroup;
import guowei.common.group.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import guowei.guliproduct.product.entity.BrandEntity;
import guowei.guliproduct.product.service.BrandService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 17:38:07
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }
    /**
     * 用于es 查询url上所有品牌信息
     */
    @RequestMapping("/infos")
    public R info(@RequestParam("brandIds") List<Long> brandIds){
        List<BrandEntity> brands = brandService.getByIds(brandIds);
        return R.ok().put("brands", brands);
    }

    /**
     * 保存品牌，并获取检验的错误结果   定义了统一异常处理，就不需要在捕获异常了
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
//        if(result.hasErrors()){
//            HashMap<String, String> map = new HashMap<>();
//            result.getFieldErrors().forEach((item)->{
//                String message = item.getDefaultMessage();
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error(400,"提交的数据不合法").put("data",map);
//        }else {
//
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改是，也需要修改与其有关联的字段 比如 CategoryBrandRelation的brandName字段
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateDetails(brand);

        return R.ok();
    }

    /**
     * 只是修改显示状态的方法。
     * @param brand
     * @return
     */
    @RequestMapping("/updateStatus")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }
    @PostMapping("/deleteOne")
    public R deleteOne(@RequestBody BrandEntity brand){
        brandService.removeById(brand.getBrandId());
        return R.ok();
    }

}
